package com.sx.pipeline.ui.provide

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AddInquiryDTO
import com.sx.pipeline.repos.http.req.AuditTaskDTO
import com.sx.pipeline.repos.http.res.*

/**
 *    author : zhouyouqiang
 *    e-mail : 1638473898@qq.com
 *    date   : 2022/5/20 13:51
 *    desc   :
 *    version: 1.0
 */
class ProvideVM(app: Application) : CYBaseViewModel(app) {
    /**
     * 获取供应列表
     */
    var page = 1
    val provideListEvent: SingleLiveEvent<Ipage<ReleaseSupplyVo>> = SingleLiveEvent()
    fun takePartSupplyList(key: String) {
        addSubscribe(
            mModel.takePartSupplyList(key, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<ReleaseSupplyVo>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<ReleaseSupplyVo>) {
                        provideListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    /**
     * 获取供应详情
     */
    val supplyDetailEvent: SingleLiveEvent<SupplyDetailVo> = SingleLiveEvent()
    fun getSupplyOrderDetail(id: Int) {
        addSubscribe(
            mModel.getSupplyOrderDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<SupplyDetailVo, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: SupplyDetailVo) {
                        supplyDetailEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                        finish()
                    }
                })
        )
    }


    /**
     * 获取供应配置数据
     */
    val taskListEvent: SingleLiveEvent<TaskConfigVo> = SingleLiveEvent()

    //供应标题
    val supplyTitleData: MutableList<String> = ArrayList()

    //计量单位
    val supplyUnitData: MutableList<String> = ArrayList()

    //结算方式
    val settleTypeData: MutableList<String> = ArrayList()

    fun getSupply() {
        addSubscribe(
            mModel.getSupply()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<TaskConfigVo, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: TaskConfigVo) {
                        taskListEvent.postValue(result)
                        for (data in result.SUPPLY_TITLE) {
                            supplyTitleData?.add(data.name)
                        }
                        for (data in result.UNIT_OF_MEASUREMENT) {
                            supplyUnitData?.add(data.name)
                        }
                        for (data in result.SETTLEMENT_METHOD) {
                            settleTypeData?.add(data.name)
                        }

                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }


    /**
     * 添加询价
     * @param orderId 订单id
     * @param contact 联系人
     * @param contactPhone 联系电话
     * @param quantity 供应数量
     * @param unit 计量单位
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    val auditSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun addInquiry(
        orderId: Int,
        contact: String,
        contactPhone: String,
        quantity: String,
        unit: String,
        startTime: String,
        endTime: String,
        payword: String
    ) {


        val dto = AddInquiryDTO(
            orderId,
            contact,
            contactPhone,
            quantity,
            unit,
            startTime,
            endTime,
            payword
        )
        addSubscribe(
            mModel.addInquiry(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("添加询价成功")
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    /**
     * 供应询价
     */
    val providePriceEvent: SingleLiveEvent<Double> = SingleLiveEvent()
    fun getInquiryPrice() {
        addSubscribe(
            mModel.getInquiryPrice()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Double, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Double) {
                        providePriceEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}